source ~/.bash_profile

# ssh配置
DEPLOY_USER=root
DEPLOY_PORT=22
# 钉钉告警配置
ACCESS_TOKEN=e9526eb327a1c1d731518db1593c623bcd240ee3fd4f0dee0feb63d0dabd6fe6

for DEPLOY_HOST in ${DEPLOY_HOSTS[@]}; do

# 检查mvn打包是否成功
if [ ! -f "${SERVER_JAR_PATH}" ]; then
  echo "mvn编译打包失败. ${SERVER_JAR_PATH} 不存在."
  exit -1
fi

# 钉钉提醒-部署开始
curl -fsSL https://gitee.com/hdzhangQAQ/twisly-riptide-doc/raw/master/shell/deploy/tool_send_ding.sh | \
  SERVER_LABEL=后端项目 \
  SERVER_NAME=${SERVER_NAME} \
  PROCESS=部署开始 \
  DEPLOY_HOST=${DEPLOY_HOST} \
  PROFILE=${PROFILE} \
  BUILD_USER=${BUILD_USER} \
  ACCESS_TOKEN=${ACCESS_TOKEN} \
sh -

# 文件备份
DATE=$(date +%Y%m%d%H%M)
ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} <<EOF
  mkdir -p ${REMOTE_DEPLOY_PATH}/backup

  if [ -f "${REMOTE_DEPLOY_PATH}/${SERVER_NAME}.jar" ]; then
    echo "${SERVER_NAME}.jar 备份..."
      # cp ${REMOTE_DEPLOY_PATH}/${SERVER_NAME}.jar ${REMOTE_DEPLOY_PATH}/backup/${SERVER_NAME}-$DATE.jar
      echo "备份 ${SERVER_NAME}.jar 完成"
  else
    echo "${REMOTE_DEPLOY_PATH}/${SERVER_NAME}.jar 不存在, 跳过备份"
  fi
EOF

# 文件传输
scp -P ${DEPLOY_PORT} -r ${SERVER_JAR_PATH} ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DEPLOY_PATH}
scp -P ${DEPLOY_PORT} -r ${SERVER_FOLDER}/${SERVER_NAME}/Dockerfile ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DEPLOY_PATH}

# 运行docker容器
# 容器id
CONTAINER_ID=$(ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} docker ps -a | grep ${SERVER_NAME} | awk '{print $1}')
ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} <<EOF
  if [ -n "${CONTAINER_ID}" ]; then
    echo "存在${SERVER_NAME}容器, CONTAINER_ID=${CONTAINER_ID}, 停止容器..."
    docker stop ${CONTAINER_ID}
    echo "容器${SERVER_NAME}-${CONTAINER_ID}停止成功"

    echo "存在${SERVER_NAME}容器, CONTAINER_ID=${CONTAINER_ID}, 开始删除..."
    docker rm ${CONTAINER_ID}
    echo "容器${SERVER_NAME}-${CONTAINER_ID}删除成功"
  fi
EOF

# 构建docker镜像
# 镜像id
IMG_ID=$(ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} docker images | grep ${SERVER_NAME} | awk '{print $3}')
ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} <<EOF
  if [ -n "${IMG_ID}" ]; then
    echo "存在${SERVER_NAME}镜像, IMG_ID=${IMG_ID}, 开始删除..."
    docker rmi ${IMG_ID}
    echo "历史镜像${SERVER_NAME}-${IMG_ID}删除成功"
  fi
  echo "开始构建镜像${SERVER_NAME} ..."
  cd ${REMOTE_DEPLOY_PATH}
  docker build -t ${SERVER_NAME} -f Dockerfile .
EOF

# 运行docker容器
# 容器id
CONTAINER_ID=$(ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} docker ps -a | grep ${SERVER_NAME} | awk '{print $1}')
ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} <<EOF
  echo "开始创建容器${SERVER_NAME} ..."
  docker run -e TZ=Asia/Shanghai -e JAVA_OPTS='${JAVA_OPTS}' -e PROFILE='${PROFILE}' --name ${SERVER_NAME} -p ${SERVER_PORT}:${SERVER_PORT} -v /data/logs:/data/logs -d ${SERVER_NAME}
  echo "${SERVER_NAME}容器创建完成"
EOF

# 钉钉提醒-部署成功
curl -fsSL https://gitee.com/hdzhangQAQ/twisly-riptide-doc/raw/master/shell/deploy/tool_send_ding.sh | \
  SERVER_LABEL=后端项目 \
  SERVER_NAME=${SERVER_NAME} \
  PROCESS=部署成功 \
  DEPLOY_HOST=${DEPLOY_HOST} \
  PROFILE=${PROFILE} \
  BUILD_USER=${BUILD_USER} \
  ACCESS_TOKEN=${ACCESS_TOKEN} \
sh -

done
